@import 'sass/shared';
@import 'sass/chapter';
@import 'sass/contents';
@import 'sass/sign-up';
@import 'sass/print';

// Make sure we don't split on the thin spaces around an em dash.
.emdash {
  white-space: nowrap;
}

.scrim {
  position: absolute;
  width: 100%;
  height: 10000px;

  z-index: 4;

  //background: url('columns.png');
  background: url('rows.png');
}

a {
  color: $primary;
  text-decoration: none;
  outline: none;

  border-bottom: solid 1px transparentize($light, 1.0);

  transition: color 0.2s ease,
              border-color 0.4s ease;
}

a:hover {
  color: $primary;
  border-bottom: solid 1px opacify($light, 1.0);
}

nav {
  font: 300 15px/24px $nav;
  background: $dark;
  color: $gray-40;

  a, h2 a {
    color: $gray-60;
    text-decoration: none;
    border-bottom: none;
  }

  a:hover {
    color: $light;
    text-decoration: none;
    border-bottom: none;
  }

  img {
    box-sizing: border-box;
    width: 100%;
    padding: 110px $col 23px $col;
  }

  h2 {
    font: 400 16px/24px $nav;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: $gray-60;
  }

  h3 {
    font: 400 18px/24px $nav;
    color: $gray-60;
  }

  h2 small, h3 small {
    float: right;
    font-size: 16px;
    color: $gray-40;
  }

  ol, ul {
    margin: 6px 0 3px 0;
    padding: 6px 0 4px 24px;
    border-top: solid 1px $gray-30;
    border-bottom: solid 1px $gray-30;
  }

  ul {
    list-style-type: none;
    padding-left: 0;
  }

  li small {
    float: right;
    font-size: 14px;
    color: $gray-40;
  }

  li.divider {
    margin: 5px 0 7px 0;
    border-top: solid 1px $gray-30;
  }

  li.end-part {
    font-size: 12px;
    font-weight: 400;
    text-transform: uppercase;
    letter-spacing: 1px;

    small {
      font-weight: 300;
      text-transform: none;
      letter-spacing: 0;
    }
  }

  .prev-next {
    padding-top: 7px;
    font: 400 12px/18px $nav;
    text-align: center;
    text-transform: uppercase;
    letter-spacing: 1px;
  }
}

nav.wide {
  position: fixed;
  width: $col * 7;
  height: 100%;

  .contents {
    margin: 24px $col;
  }
}

// This is needed to make the nav fixed (not scrolling with the content) but
// still positioned horizontally based on the page.
// See: http://stackoverflow.com/a/11833892/9457
.nav-wrapper {
  position: absolute;
  right: $col * 6;
}

// For medium-sized screens, the navigation floats over the same column as the
// asides.
nav.floating {
  // Only shown on narrower screens.
  display: none;

  z-index: 2;
  position: absolute;
  width: $col * 6;

  border-bottom-left-radius: 3px;
  border-bottom-right-radius: 3px;

  #expand-nav {
    padding: 0 0 4px 0;
    display: block;
    font-size: 20px;
    text-align: center;
    color: $gray-40;
    cursor: pointer;

    transition: padding 0.2s ease,
                margin 0.2s ease,
                color 0.2s ease;
  }

  #expand-nav, #expand-nav:hover {
    border-bottom: none;
  }

  #expand-nav:hover {
    color: $light;
  }

  .expandable {
    overflow: hidden;
    padding: 0 12px;

    // Using max-height instead of height to allow the list to navigation to
    // automatically choose its height based on the size of the list while
    // still transitioning.
    // See: http://stackoverflow.com/a/8331169/9457
    max-height: 0;
    transition: margin 0.2s ease,
                max-height 1.0s ease;

    .prev-next {
      padding-bottom: 6px;
    }
  }

  .expandable.shown {
    // This should be as small as possible while still being large enough for
    // the worst case chapter.
    max-height: 550px;
  }

  img {
    padding: 110px $col/2 23px $col/2;
  }
}

nav.floating.pinned {
  position: fixed;

  top: -85px;

  .expandable {
    margin-top: -13px;
  }

  #expand-nav {
    margin-top: -14px;
  }
}

nav.narrow {
  display: none;

  text-align: center;

  img {
    box-sizing: content-box;
    padding: 11px 0 3px 0;
    width: auto;
    height: 27px;
  }

  .prev, .next {
    font-size: 32px;
    position: absolute;
    top: 12px;
    padding: 0 $col;
  }

  .prev {
    left: 0;
  }

  .next {
    right: 0;
  }
}

.left {
  float: left;
}

.right {
  float: right;
}

.page {
  position: relative;

  width: $col * 19;
  margin: 0 auto 0 $col * 8;
}

// Make em dashes look pretty. Goals:
//
// - Add a tiny bit of space on either side. Completely unspaced em dashes
//   look too tight to me.
// - Allow an em dash at the end of a line.
// - Prevent an em dash at the beginning of a line.
//
// Wrapping each `&mdash;` in a span with this class and consuming the
// preceding whitespace seems to accomplish that.
.em {
  padding: 0 .1em;
  white-space: nowrap;
}

code, .codehilite {
  color: $warm-dark;
  background: $warm-light;
  border-radius: 3px;
}

code {
  font: normal 15px $mono;
  white-space: nowrap;
  padding: 2px;
}

a code {
  color: $primary;
}

.codehilite {
  padding: 12px;
  margin: -12px;

  // Scroll horizontally if not wide enough.
  overflow: auto;
}

pre {
  font: normal 13px/20px $mono;
  margin: 0;
  padding: 0;
}

// If aside is right before a code block (when the asides are inline), make
// sure they don't overlap.
aside + div.codehilite {
  margin-top: 12px;
}

div.codehilite + aside {
  margin-top: 24px;
}

// If the chapter ends with code, don't overlap the challenges box.
div.codehilite + div.challenges {
  margin-top: $col / 2;
}

article {
  position: relative;
  width: $col * 12;

  h1 {
    position: relative;
    font: 48px/48px $serif;
    padding: 109px 0 19px 0;
    z-index: 2;
  }

  h1.part {
    font: 600 36px/48px $nav;
    padding: 108px 0 20px 0;
    text-transform: uppercase;
    letter-spacing: 1px;
  }

  .number {
    position: absolute;
    top: 50px;
    right: 0;
    z-index: 1;

    font: 300 96px $nav;
    color: $light;
  }

  p {
    margin: 24px 0;
  }

  ol, ul {
    margin: 24px 0;
    padding: 0 0 0 24px;
  }

  img {
    max-width: 100%;
  }

  img.wide {
    max-width: none;
    width: $col * 19;
  }
}

aside {
  position: absolute;
  right: -$col * 7;
  width: $col * 6;

  font: normal 14px/20px $serif;

  border-top: solid 1px $light;

  p {
    margin: 20px 0;
  }

  p:first-child {
    margin-top: 4px;
  }

  p:last-child {
    margin-bottom: 4px;
  }

  code {
    font-size: 14px;
    border-radius: 2px;
    padding: 1px 2px;
  }

  .codehilite {
    padding: 6px;
    margin: -12px 0;
  }

  img.above {
    position: absolute;
    bottom: 100%;
    margin-bottom: 16px;
  }

  blockquote {
    margin: 20px 0;

    &::before, &::after {
      content: none;
    }

    p {
      margin: 0 12px;
      font: italic 15px/20px $serif;
    }
  }
}

// Sometimes there isn't room to hang the aside *down* next to the content it's
// annotating, so support asides where the bottom is aligned with the content.
aside.bottom {
  border-top: none;
  border-bottom: solid 1px $light;
}

blockquote {
  position: relative;

  margin: 29px 0 31px 0;

  &::before, &::after {
    position: absolute;
    top: -20px;
    font: italic 72px $serif;
    color: $light;
  }

  &::before {
    content: "\201c";
    left: -7px;
  }

  &::after {
    content: "\201d";
    right: 8px;
  }

  p {
    margin: 0 $col;
    font: italic 24px/36px $serif;
    color: $gray-50;
  }

  cite {
    display: block;
    text-align: right;
    color: $gray-60;
    font-size: 18px;

    &::before {
      content: "\2014\00a0";
      color: $light;
    }
  }
}

footer {
  position: relative;
  border-top: solid 1px $light;
  color: $gray-60;
  font: 400 15px $nav;
  text-align: center;
  margin: 48px 0;
  padding-top: 48px;

  a, a:hover {
    border: none;
  }

  .next {
    position: absolute;
    right: 0;
    top: -13px;

    padding-left: 4px;
    background: #fff;

    font: 400 17px/24px $nav;
    text-transform: uppercase;
    letter-spacing: 1px;
  }

  .next:hover {
    color: $primary-dark;
    border: none;
  }
}

.source-file, .source-file-narrow {
  font: normal 11px/16px $mono;
  color: $warm-60;

  em {
    color: $warm-40;
    font-style: normal;
  }
}

.source-file-narrow {
  // Don't show unless in single-column.
  display: none;

  margin: 0px -12px 0 0;
  padding: 14px 0 0 0;
  text-align: right;
}

.source-file {
  position: absolute;
  right: -$col * 7;
  width: $col * 6;
  padding: 2px 0 0 0;

  &::before {
    content: "<<";
    color: $warm-80;
    position: absolute;
    left: -($col - 12px);
    width: $col - 12px;
    text-align: center;
  }
}

// Syntax highlighting.
.codehilite {
  pre { color: mix($warm-light, $warm-dark, 20%); }

  // Keyword.
  .k, .kd, .kt, .kn, .kr  { color: hsl(200, 100%, 45%); }

  // Number.
  .mi, .mf                { color: hsl( 20, 70%, 55%); }

  // String.
  .s, .sc, .s2            { color: hsl( 40, 70%, 45%); }

  // Comment.
  .c, .c1, .cm            { color: mix($warm-light, $warm-dark, 50%); }

  // Preprocessor, annotation.
  .cp, .nd                { color: hsl(290, 60%, 60%); }

  // Label (private: etc.).
  .nl, .nt                { color: hsl(200, 100%, 40%); }

  // Name.
  .n, .na, .nc, .nn, .nf  { color: hsl(200, 100%, 35%); }

  // Type name.
  .vg, .nc                { color: hsl(180, 100%, 35%); }

  // Built-in name ("this", etc.).
  .nb                     { color: hsl(200, 100%, 45%); }

  .insert {
    margin: -2px -12px;
    padding: 2px 10px;
    border-left: solid 2px $warm-80;
    border-right: solid 2px $warm-80;
    background: $warm-95;

    // Make sure the background is as wide as needed even when it overflows and
    // needs to scroll.
    display: inline-block;
    min-width: 100%;
  }

  .delete {
    margin: -2px -12px;
    padding: 2px 10px;
    border-left: solid 2px $warm-80;
    border-right: solid 2px $warm-80;
    // Hatched lines.
    background: repeating-linear-gradient(
      -45deg,
      $warm-80,
      $warm-80 1px,
      rgba(0, 0, 0, 0.0) 1px,
      rgba(0, 0, 0, 0.0) 6px
    );

    span {
      color: $warm-60;
    }
  }

  // Snippets of code before and after real code to show where to insert it.
  .insert-before span, .insert-after span {
    color: $warm-60;
  }

  // When we just add a trailing comma to a line, highlight it specially.
  .insert-before .insert-comma {
    margin: -2px -1px;
    padding: 2px 1px;
    border-radius: 2px;

    background: $warm-95;
    color: $warm-dark;
  }
}

// On a not-entirely-large screen, don't show the fixed nav on the left.
@media only screen and (max-width: 1344px) {
  nav.wide { display: none; }
  nav.floating { display: block; }

  body {
    margin: 0 24px;
  }

  .page {
    position: relative;
    width: inherit;
    max-width: $col * 19;
    margin: 0 auto;
  }

  article {
    width: inherit;
    margin-right: $col * 7;
  }
}

// Tablets and mobile go single column.
@media only screen and (max-width: $col * 20) {
  body {
    margin: 0;
  }

  .page {
    max-width: $col * 12;
  }

  nav.floating {
    display: none;
  }

  nav.narrow {
    display: block;
  }

  .page {
    margin: 0 $col;
    width: inherit;
  }

  article {
    margin: 0;

    // Size wide images to fit inside the column again.
    img.wide {
      width: inherit;
      max-width: 100%;
    }
  }

  aside {
    position: inherit;
    right: inherit;
    width: inherit;

    border-bottom: solid 1px $light;

    p:first-child {
      margin-top: 8px;
    }

    p:last-child {
      margin-bottom: 8px;
    }

    // Make sure aside images don't get too big when the asides are inlined
    // in single column mode.
    img {
      display: block;
      max-width: $col * 6;
      margin: 0 auto;
    }

    img.above {
      position: relative;
    }
  }

  .source-file {
    display: none;
  }

  .source-file-narrow {
    display: block;
  }
}

// Then bring the margins in some.
// The cut-off sizes here are based on trying to get 72 columns of code to fit.
@media only screen and (max-width: 630px) {
  .page {
    margin: 0 $col / 2;
    width: inherit;
  }

  nav.narrow {
    .prev, .next {
      padding: 0 $col / 2;
    }
  }
}

// Finally, shrink the grid to 22px and shrink the text.
@media only screen and (max-width: 580px) {
  body {
    font-size: 15px;
    line-height: 22px;
  }

  .scrim {
    background: url('rows-22.png');
  }

  nav.narrow {
    img {
      padding: 9px 0 1px 0;
      height: 27px;
    }

    .prev, .next {
      top: 11px;
    }
  }

  article {
    h1 {
      font-size: 36px;
      padding: 100px 0 14px 0;
    }

    h1.part {
      font-size: 30px;
      padding: 97px 0 17px 0;
    }

    .number {
      top: 61px;
      font-size: 72px;
    }

    p {
      margin: 22px 0;
    }

    ol, ul {
      margin: 22px 0;
      padding: 0 0 0 22px;
    }
  }

  blockquote {
    margin: 27px 0 28px 0;

    &::before, &::after {
      top: -17px;
      font-size: 52px;
    }

    p {
      margin: 0 22px;
      font-size: 20px;
      line-height: 33px;
    }
  }

  footer {
    .next {
      font-size: 15px;
    }
  }
}
